package com.team.easybuy.service.Impl;

import com.team.easybuy.entity.UserAddress;
import com.team.easybuy.mapper.AddressMapper;
import com.team.easybuy.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;
    @Override
    public List<UserAddress> getUserAddressList(Integer userId) {
        return addressMapper.selectUserAddressListByUserId(userId);
    }

    @Override
    public UserAddress getUserAddress(Integer id) {
        return addressMapper.selectUserAddress(id);
    }

    @Override
    public boolean addUserAddress(UserAddress userAddress) {
        if(addressMapper.countUserAddressByUserId(userAddress.getUserId())>=5){
            return false;
        }
        userAddress.setCreateTime(new Date());
        userAddress.setIsDefault(0);
        int count = addressMapper.insertUserAddress(userAddress);
        if(count == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUserAddress(Integer id) {
        int count = addressMapper.deleteUserAddress(id);
        if(count == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean editUserAddress(UserAddress userAddress) {
        int count = addressMapper.updateUserAddress(userAddress);
        if(count == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean setDefaultAddress(UserAddress userAddress) {

        addressMapper.updateUserAddressisDefault(userAddress.getUserId());
        int count = addressMapper.updateUserAddress(userAddress);
        if(count == 1){
            return true;
        }
        return false;
    }

    @Override
    public int getCountUserAddressByUserId(Integer userId) {
        return addressMapper.countUserAddressByUserId(userId);
    }
}
